package bo;

import java.io.Serializable;
import java.util.Comparator;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@SuppressWarnings("serial")
@Entity(name = "TeamSeason")
public class TeamSeason implements Serializable {
	@EmbeddedId
	TeamSeasonId id;

	@Embeddable
	static class TeamSeasonId implements Serializable {
		@ManyToOne
		@JoinColumn(name = "teamId", referencedColumnName = "teamId", insertable = false, updatable = false)
		Team team;
		@Column
		Integer year;
		@Override
		public boolean equals(Object obj) {
			if(!(obj instanceof TeamSeasonId)){
				return false;
			}
			TeamSeasonId other = (TeamSeasonId)obj;
			// in order for two different object of this type to be equal,
			// they must be for the same year and for the same team
			return (this.team==other.team &&
					this.year==other.year);
		}
		 
		@Override
		public int hashCode() {
			Integer hash = 0;
			if (this.team != null) hash += this.team.hashCode();
			if (this.year != null) hash += this.year.hashCode();
			return hash;
		}
	}

	@Column
	int gamesPlayed;
	@Column
	int wins;
	@Column
	int losses;
	@Column
	int rank;
	@Column
	int totalAttendance;
	
	public TeamSeason() {}
	
	public TeamSeason(Team t, Integer year) {
		TeamSeasonId psi = new TeamSeasonId();
		psi.team = t;
		psi.year = year;
		this.id = psi;
	}
		
	//GETTERS
	public TeamSeasonId getId() {
		return this.id;
	}
	public int getGamesPlayed() {
		return gamesPlayed;
	}
	
	public void setYear(int year) {
		this.id.year = year;
	}
	public int getYear() {
		return this.id.year;
	}
	
	public void setTeam(Team team) {
		this.id.team = team;
	}
	public Team getTeam() {
		return this.id.team;
	}
	
	public int getWins() {
		return wins;
	}
	
	public int getLosses() {
		return losses;
	}
	
	public int getRank() {
		return rank;
	}
	
	public int getTotalAttendance() {
		return totalAttendance;
	}
	//END GETTERS
	
	//SETTERS
	public void setGamesPlayed(int newVal) {
		gamesPlayed = newVal;
	}
	
	public void setWins(int newVal) {
		wins = newVal;
	}
	
	public void setLosses(int newVal) {
		losses = newVal;
	}
	
	public void setRank(int newVal) {
		rank = newVal;
	}
	
	public void setTotalAttendance(int newVal) {
		totalAttendance = newVal;
	}
	//END SETTERS
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof TeamSeason)){
			return false;
		}
		TeamSeason other = (TeamSeason)obj;
		return other.getId().equals(this.getId());
	}
	 
	@Override
	public int hashCode() {
		return this.getId().hashCode();
	}
	
	public static Comparator<TeamSeason> teamSeasonsComparator = new Comparator<TeamSeason>() {

		public int compare(TeamSeason ps1, TeamSeason ps2) {
			Integer year1 = ps1.getYear();
			Integer year2 = ps2.getYear();
			return year1.compareTo(year2);
		}

	};
}
